package com.saint.libs.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.os.SystemClock;


/**
 * 倒计时的类等
 * @author Android
 *
 */
public class TimeTaskUtils {

	private static Handler handler ;
	private static HandlerThread handlerThread ;
	private static int number;
	
	public interface TimeTaskCallBack{
		public void callBack();
	}
	
	private TimeTaskUtils() {
	}
	
	/**
	 * 周期性调用, 利用HandlerThread计时.
	 * 适合短期的周期
	 * =======>这个不能直接用, 因为静态的方法全局只能用一个, 可以用{@code TimeTaskUtils#cyclicity2Demo(TimeTaskCallBack, long)}
	 * @param callback
	 * @param delayMillis
	 */
	public static void cyclicity(final TimeTaskCallBack callback, final long delayMillis){
		if(handlerThread == null) {
			handlerThread = new HandlerThread("TimerTask");
			handlerThread.start();
		}
		if(handler == null) {
			handler = new Handler(handlerThread.getLooper()){
				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					callback.callBack();
					sendEmptyMessageDelayed(0x123, delayMillis);
				}
			};
			handler.sendEmptyMessageDelayed(0x123, delayMillis);
		}
	}
	
	
	/**
	 * 每次调用都会创建一个周期循环的东西, 且还不能停止
	 * @param callback
	 * @param delayMillis
	 */
	public static void cyclicity2Demo(final TimeTaskCallBack callback, final long delayMillis) {
		HandlerThread handlerThread2 = new HandlerThread("TimerTask" + number++);
		handlerThread2.start();
		Handler handler = new Handler(handlerThread2.getLooper()){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				callback.callBack();
				sendEmptyMessageDelayed(0x124, delayMillis);
			}
		};
		handler.sendEmptyMessageDelayed(0x124, delayMillis);
	}
	
	/**
	 * 适合长时间的周期------------------还有问题
	 * @param callBack
	 * @param service
	 */
	public static void cyclicityWithAlarm(String action, Context context, Class<?> clz, long intervalMillis) {
		Intent intent = new Intent(context, clz);
		intent.setAction(action);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		// 开始时间
		long firsttime = SystemClock.elapsedRealtime();
		AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		// 发送一个周期
		am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, firsttime, intervalMillis, pendingIntent);
	}
	
	/**
	 * 取消Alarm周期------------------还有问题
	 * @param callBack
	 * @param service
	 */
	public static void cyclicityWithAlarmCancle(String action, Context context, Class<?> clz, long intervalMillis) {
		Intent intent = new Intent(context, clz);
		intent.setAction(action);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		// 开始时间
//		long firsttime = SystemClock.elapsedRealtime();
		AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		// 发送一个周期
//		am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, firsttime, intervalMillis, pendingIntent);
		am.cancel(pendingIntent);
	}
	
	
	
	public static void dispose() {
		if(handler != null) {
			handler = null;
		}
		if(handlerThread != null) {
			handlerThread.quit();
			handlerThread = null;
		}
	}
	
}
